from URLParser import URLParser
from Formats import OutputTemplate
from FRCFeed import FRCEvent,GetTeamHistory,GetTeamInfo
from Debug import *

class MainHandler:
	def __init__(self,url):
		self.URL = URLParser(url)
		self.URL.Parse()
	
	def CreateReponse(self):
		self.Event = FRCEvent(self.URL.Event,self.URL.Year)
		if len(self.URL.Error) > 0:
			return
		MyTemplate = OutputTemplate(self.URL.GetFormatPath())
		if self.URL.What == 'rankings':
			self.Event.GetRankings()
			return MyTemplate.Generate(Body=self.Event.Rankings)
		if self.URL.What == 'awards':
			self.Event.GetAwards()
			return MyTemplate.Generate(Body=self.Event.Awards)
		if self.URL.What == 'schedule':
			if self.URL.When == 'qualification':
				self.Event.GetQualificationSchedule()
				return MyTemplate.Generate(Body=self.Event.QualificationSchedule)
			if self.URL.When == 'elimination':
				self.Event.GetEliminationSchedule()
				return MyTemplate.Generate(Body=self.Event.EliminationSchedule)
		if self.URL.What == 'team_info':
			MyTeamHistoryTemplate = OutputTemplate(self.URL.GetFormatTemplate()%("Team_History"))
			return MyTemplate.Generate(Header=GetTeamInfo(self.URL.TeamNo,MyTeamHistoryTemplate))
		if self.URL.What == 'team_list':
			DebugPrint("Event Code="+self.URL.Event)
			self.Event.GetTeamList()
			return MyTemplate.Generate(Body=self.Event.TeamList)
		if self.URL.What == 'team_history':
			return MyTemplate.Generate(Body=GetTeamHistory(self.URL.TeamNo))
		if self.URL.What == 'results':
			if self.URL.When == 'qualification':
				self.Event.GetQualificationResults()
				return MyTemplate.Generate(Body=self.Event.QualificationResults)
			if self.URL.When == 'elimination':
				self.Event.GetEliminationResults()
				return MyTemplate.Generate(Body=self.Event.EliminationResults)
	